package idv.jieh.commons.util;

public class TaiwanIDChecker {

  private static int[] code = {10, 11, 12, 13, 14, 15, 16, 17, 34, 18, 19, 20, 21, 22, 35, 23, 24,
                              25, 26, 27, 28, 29, 32, 30, 31, 33};
  public static boolean isAvailable(String id) {
    if(id.length() != 10 ||
       !idv.jieh.commons.util.DataAnalysis.isLetter(id.substring(0,1)) ||
       !idv.jieh.commons.util.DataAnalysis.isDigit(id.substring(1,10))){
      return false;
    }
    id = id.toUpperCase();
    int y = (code[id.charAt(0) - 65] / 10) + ((code[id.charAt(0) - 65] % 10) * 9);
    for (int i = 8; i > 0; i--)
      y += ((int) id.charAt(9 - i) - 48) * i;
    y += ((int) id.charAt(9) - 48);
    if ((y % 10) == 0) {
      return true;
    } else {
      return false;
    }
  }

}
